package model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.util.List;

import org.apache.xerces.xni.parser.XMLDocumentFilter;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
import org.cyberneko.html.HTMLConfiguration;
import org.cyberneko.html.filters.ElementRemover;
import org.cyberneko.html.filters.Writer;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.w3c.dom.Document;

public class LyricsPage extends PageCrawled {
	
	private static final String TITULO_XPATH = "//div/h1[@id='identificador_musica']";
	private static final String ARTISTA_XPATH = "//div/h2/a[@id='identificador_artista']";
	private static final String LETRA_XPATH = "//div[@id='div_letra']/p";
	private static final String CONTEUDO_XPATH = "//div/p";
	private static final String TITULO_PAGINA_XPATH = "//head/title";
	
	private String singer;
	private String lyric;
	private String title;
	
	
	public LyricsPage(URL url) {
		super(url);
		// TODO Auto-generated constructor stub
	}
	
	public String getSinger() {
		return singer;
	}

	public void setSinger(String singer) {
		this.singer = singer;
	}

	public String getLyric() {
		return lyric;
	}

	public void setLyric(String lyric) {
		this.lyric = lyric;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
/*
	@Override
	public void loadAtts(){
		String contentWithNoSpaces = this.content.toLowerCase().replaceAll("\\s", " ");
		int index = 0;
		Vector<String> links = new Vector<String>();
		Boolean singerGot = false;
		Boolean titleGot = false;
		Boolean lyricGot = false;
		while ((index = contentWithNoSpaces.indexOf("<div ", index)) != -1)
		{
		    if (!titleGot && (index = contentWithNoSpaces.indexOf("identificador_musica", index)) == -1) break;
		    if (!singerGot && titleGot && (index = contentWithNoSpaces.indexOf("identificador_artista", index)) == -1) break;
		    if (!lyricGot && singerGot && (index = contentWithNoSpaces.indexOf("div_letra", index)) == -1) break;
		    if ((index = contentWithNoSpaces.indexOf(">", index)) == -1) break;
		    String remaining = this.content.substring(++index);
		    StringTokenizer st 
				= new StringTokenizer(remaining, "\t\n\r<#");
		    String strLink = st.nextToken();
		    
		   
		    if(!lyricGot && singerGot){
		    	index = 0;
		    	lyricGot = true;
		    	setLyric(strLink);
		    }
		    if(!singerGot && titleGot){
		    	index = 0;
		    	singerGot = true;
		    	setSinger(strLink);
		    }
		    if(!titleGot){
		    	index = 0;
		    	titleGot = true;
		    	setTitle(strLink);
		    }
		    
//			if (! links.contains(strLink)) links.add(strLink);
		}
	}
	*/

	@Override
	public void loadAtts() {
		// TODO Auto-generated method stub
		HtmlCleaner cleaner = new HtmlCleaner();
		CleanerProperties props = cleaner.getProperties();
		props.setAllowHtmlInsideAttributes(true);
		props.setAllowMultiWordAttributes(true);
		props.setRecognizeUnicodeChars(true);
		props.setOmitComments(true);
		
		Document doc = null;
		try {
			TagNode page = cleaner.clean(url);
			Object[] node_titulos = page.evaluateXPath(TITULO_XPATH);
			Object[] node_artistas = page.evaluateXPath(ARTISTA_XPATH);
			Object[] node_letras = page.evaluateXPath(LETRA_XPATH);
			Object[] node_titulos_pagina = page.evaluateXPath(TITULO_PAGINA_XPATH);
			Object[] node_conteudos = page.evaluateXPath(CONTEUDO_XPATH);
			
			if(node_titulos.length > 0){
				TagNode node = (TagNode) node_titulos[0];
				String titulo = node.getChildren().iterator().next().toString().trim();
				setTitle(titulo);
			}
			
			if(node_artistas.length > 0){
				TagNode node = (TagNode) node_artistas[0];
				String artista = node.getChildren().iterator().next().toString().trim();
				setSinger(artista);
			}
			
			if(node_letras.length > 0){
				String letra = "";
				for (Object obj : node_letras) {
					TagNode node = (TagNode) obj;
					letra += node.getText() + "\n";
				}
				setLyric(letra);
			}
			
			if(node_conteudos.length > 0){
				String conteudo = "";
				for (Object obj : node_conteudos) {
					TagNode node = (TagNode) obj;
					conteudo += node.getText() + "\n";
				}
				setContent(conteudo);
			}
			
			if(node_titulos_pagina.length > 0){
				TagNode node = (TagNode) node_titulos_pagina[0];
				String tituloPagina = node.getChildren().iterator().next().toString().trim();
				setTituloPagina(tituloPagina);
			}
			
//			doc  = cleaner.c
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println("Erro!");
		}
	}
}
